package tech.gismap.admin.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tech.gismap.admin.domain.DaTableType;
import tech.gismap.admin.web.bo.DaTableTypeBo;
import tech.gismap.admin.web.vo.DaTableTypeVo;
import tech.gismap.admin.mapper.DaTableTypeMapper;
import tech.gismap.common.mybatis.core.page.PageQuery;
import tech.gismap.common.mybatis.core.page.TableDataInfo;

/**
* 数据字典
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class DaTableTypeService {

    private final DaTableTypeMapper baseMapper;

    public TableDataInfo<DaTableTypeVo> selectPageList(DaTableTypeBo bo, PageQuery pageQuery) {
        Page<DaTableTypeVo> page = baseMapper.selectVoPage(pageQuery.build(), this.buildQueryWrapper(bo));
        return TableDataInfo.build(page);
    }

    private Wrapper<DaTableType> buildQueryWrapper(DaTableTypeBo bo) {
        LambdaQueryWrapper<DaTableType> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(bo.getBusiness() != null, DaTableType::getBusiness, bo.getBusiness());
        wrapper.eq(bo.getType() != null, DaTableType::getType, bo.getType());
        wrapper.orderByDesc(DaTableType::getSort);
        return wrapper;
    }
}
